package com.cn.test;

import com.cn.dao.BookDaoImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InstanceTest2 {
    /**
     * 4.1 无参构造器创建对象
     */
    @Test
    public void test1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDaoImpl bookDao = context.getBean("bookDao", BookDaoImpl.class);
        bookDao.save();
    }
    /**
     * 4.2 静态工厂创建对象
     */
    @Test
    public void test2(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDaoImpl bookDao = context.getBean("bookDao", BookDaoImpl.class);
        bookDao.save();
    }
    /**
     * 4.3 实例工厂创建对象
     */
    @Test
    public void test3(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDaoImpl bookDao = context.getBean("bookDao", BookDaoImpl.class);
        bookDao.save();
    }
    /**
     * 4.4 FactoryBean创建对象
     */
    @Test
    public void test4(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这里调用的是bookDaoFactoryBean，但是实际是调用了bookDaoFactoryBean的getObject()方法，可以直接返回BookDaoImpl
        BookDaoImpl bookDao = context.getBean("bookDaoFactoryBean", BookDaoImpl.class);
        bookDao.save();
        bookDao.hello();
    }
}

